<?php
/**
 * 手机信息页面
 */
class Module_phoneinfo{
	function index(){
	}
	/**
	 *	注册 
	 */
	function reg(){
		$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
		$imsi = isset($_POST['imsi']) ? trim($_POST['imsi']) : '';
		$version = isset($_POST['version']) ? trim($_POST['version']) : '';
		
		if (empty($phone) || empty($imsi) || empty($version)){
			echo 'ERROR';
			return false;
		}
		
		loadLib('Phoneinfo');
		/**************查询帐号****************/
		$user = PhoneInfo::getOneUser($phone);
		if (!preg_match('%^[0-9]{11}$%' , $phone)){
			echo 'ERROR';
			return false;
		}
		if (!empty($user)){
			echo 'ERROR';
			return false;
		}
	  	$code = PhoneInfo::getAddUser($phone, $imsi, $version);
	  	
	  	if ($code == 0){
	  		echo 'OK';
	  		return false;
	  	}
		
	}
	/**
	 * 阅读信息
	 */
	function readed(){
		$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
		$imsi = isset($_POST['imsi']) ? trim($_POST['imsi']) : '';
		if (empty($phone) || !preg_match('%^[0-9]{11}$%' , $phone)){
			echo 'ERROR';
			return false;
		}
		loadLib('Phoneinfo');
		/**********************获取阅读信息************************/
		$result = PhoneInfo::getAllMsg($phone);
		foreach ($result as $k => $row){
			if ($k <= 4){
				$rs = array('ctype' => $row['ctype'], 'sender' => $row['sender'], 'dtime' => $row['dtime'], 'content' => $row['content']);
				$vstr[] = implode('|' , $rs);
				
				$id[] = $row['id'];
			}else{
				break;
			}
		}
		if (!empty($vstr)){
			//修改成已读状态
			PhoneInfo::getAlterReaded(implode(',' , $id));
			$str = implode($vstr , "\r\n");	
		}else{
			$str = '';
		}
		echo $str;
	}
	/**
	 * 发送信息初始化
	 */
	function transmitter(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('phoneinfo/transmitter.tpl.php');
	}
	/**
	 * 发送信息
	 */
	function transmitter_upload(){
		$sender = isset($_POST['sender']) ? trim($_POST['sender']) : '';
		$receiver = isset($_POST['receiver']) ? trim($_POST['receiver']) : '';
		/***************检查发送人的手机号码的有效性*******************/
		if (empty($sender) || !preg_match('%^[0-9]{11}$%' , $sender)){
			echo 'ERROR';
			return false;
		}
		/****************检查收件人手机号码的有效性********************/
		if (empty($receiver) || !preg_match('%^[0-9]{11}$%' , $receiver)) {
			echo 'ERROR';
			return false;
		}
		loadLib('Phoneinfo');
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			 $uptypes = array('text/plain',  //上传文件类型列表
			 'image/jpg',
			 'application/octet-stream');//amr类型

			//是否存在文件
			if(!empty($_FILES)){
				if (!is_uploaded_file($_FILES["upfile"]["tmp_name"])){
					echo 'ERROR';
					return false;
				}
			}
			
			$file = isset($_FILES["upfile"]) ? $_FILES["upfile"] : null;
			if(!in_array($file["type"], $uptypes))
			//检查文件类型
			{
				echo 'ERROR';
				return false;
			}
		
			$upload = date('Y/md');//按月份创建一个子文件夹
			$destination_folder = "upload/$upload/"; //上传文件路径
				
			$filename = $file["tmp_name"];
			$size = $file["size"];
			$pinfo = pathinfo($file["name"]);
			$ftype = $pinfo["extension"];
			$time = time();
			$destination = $sender ."_". $time . '.' .$ftype; //文件名字 ，发送人 + 时间 拼成
			//拼文件URL
			$path = ADMINURL.'/'.$destination_folder.$destination;  
			
			// 创建文件夹
			if(!is_dir($destination_folder)) {
				mkdir($destination_folder , 0777 , true);
				chmod($destination_folder , 0777);
			}
			//上传文件
			$up_info = move_uploaded_file($_FILES["upfile"]["tmp_name"],$destination_folder.$destination);
			
			if(empty($up_info) || !file_exists($destination_folder.$destination)) {
				echo 'ERROR';
				return false;
			}
			if ($ftype == 'txt'){
				$ctype = 0;
			}elseif($ftype == 'jpg'){
				$ctype = 1;
			}elseif($ftype == 'amr'){
				$ctype = 2;
			}else{
				echo 'ERROR';
				return false;
			}
			
			$code = PhoneInfo::getAddMsg($sender, $receiver, $ctype, $path);
			if($code === 0) {
				echo 'OK';
				return false;
			} else {
				echo 'ERROR';
				return false;
			}
		}
	}
	/**
	 * 飞信用户查询
	 */
	function fetion(){
		$str= isset($_POST['v']) ? ($_POST['v']) : '';
	//	$str='2121|22222222222';
		loadLib('Phoneinfo');
		if (!empty($str)){
			$strarr=explode('|',$str);
			foreach ($strarr as $k => $row){
				if (!empty($row)){
					$strarray[] = $row;
				}
			}
			$arr='';
			foreach ($strarray as $k => $row){
				/******************一个个检查是否存在*******************/
				$result = Phoneinfo::getOneUser($row);
				if (!empty($result)){
					$arr[] = $row;
				}
			}
		}
		if (!empty($arr)){
			$vstr = implode('|',$arr);
		}else{
			$vstr = '';
		}
		echo $vstr;
	}
}
# end of script